package org.tigris.atlas.examples.service;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Date;

import junit.framework.TestCase;

import org.tigris.atlas.examples.service.delegate.PrimitiveReturnTypeExampleServiceDelegate;
import org.tigris.atlas.examples.service.delegate.ServiceDelegateFactory;
import org.tigris.atlas.service.BigDecimalServiceResponse;
import org.tigris.atlas.service.BooleanServiceResponse;
import org.tigris.atlas.service.DateServiceResponse;
import org.tigris.atlas.service.IntegerServiceResponse;
import org.tigris.atlas.service.LongServiceResponse;
import org.tigris.atlas.service.StringServiceResponse;
import org.tigris.atlas.service.TimestampServiceResponse;
import org.tigris.atlas.service.ValueServiceResponse;
import org.tigris.atlas.service.VoidServiceResponse;

public class TestPrimitiveReturnTypes extends TestCase {

	private PrimitiveReturnTypeExampleServiceDelegate delegate;

	/**
	 * @see junit.framework.TestCase#setUp()
	 */
	protected void setUp() throws Exception {
		super.setUp();
		delegate = ServiceDelegateFactory.createPrimitiveReturnTypeExampleServiceDelegate();
	}

	/**
	 * @see junit.framework.TestCase#tearDown()
	 */
	protected void tearDown() throws Exception {
		super.tearDown();
		delegate = null;
	}
	
	//BIG DECIMAL TESTS
	public void testReturnWithBigDecimalWithoutMessages() throws Exception {
		BigDecimalServiceResponse response = delegate.returnBigDecimal(Boolean.TRUE, Boolean.FALSE, new BigDecimal("0"));
		assertPrimitiveOnly(response);
	}
	
	public void testReturnWithoutBigDecimalWithoutMessages() throws Exception {
		BigDecimalServiceResponse response = delegate.returnBigDecimal(Boolean.FALSE, Boolean.FALSE, new BigDecimal("0"));
		assertNullPrimitiveNullMessages(response);
	}	
	
	public void testReturnWithoutBigDecimalWithMessages() throws Exception {
		BigDecimalServiceResponse response = delegate.returnBigDecimal(Boolean.FALSE, Boolean.TRUE, new BigDecimal("0"));
		assertMessagesOnly(response);
	}	
	
	public void testReturnWithBigDecimalWithMessages() throws Exception {
		BigDecimalServiceResponse response = delegate.returnBigDecimal(Boolean.TRUE, Boolean.TRUE, new BigDecimal("0"));
		assertPrimitiveAndMessages(response);
	}
	
	
	//BOOLEAN TESTS
	public void testReturnWithBooleanWithoutMessages() throws Exception {
		BooleanServiceResponse response = delegate.returnBoolean(Boolean.TRUE, Boolean.FALSE, Boolean.FALSE);
		assertPrimitiveOnly(response);
	}
	
	public void testReturnWithoutBooleanWithoutMessages() throws Exception {
		BooleanServiceResponse response = delegate.returnBoolean(Boolean.FALSE, Boolean.FALSE, Boolean.FALSE);
		assertNullPrimitiveNullMessages(response);
	}	
	
	public void testReturnWithoutBooleanWithMessages() throws Exception {
		BooleanServiceResponse response = delegate.returnBoolean(Boolean.FALSE, Boolean.TRUE, Boolean.FALSE);
		assertMessagesOnly(response);
	}	
	
	public void testReturnWithBooleanWithMessages() throws Exception {
		BooleanServiceResponse response = delegate.returnBoolean(Boolean.TRUE, Boolean.TRUE, Boolean.FALSE);
		assertPrimitiveAndMessages(response);
	}
	
	
	//DATE TESTS
	public void testReturnWithDateWithoutMessages() throws Exception {
		DateServiceResponse response = delegate.returnDate(Boolean.TRUE, Boolean.FALSE, new Date());
		assertPrimitiveOnly(response);
	}
	
	public void testReturnWithoutDateWithoutMessages() throws Exception {
		DateServiceResponse response = delegate.returnDate(Boolean.FALSE, Boolean.FALSE, new Date());
		assertNullPrimitiveNullMessages(response);
	}	
	
	public void testReturnWithoutDateWithMessages() throws Exception {
		DateServiceResponse response = delegate.returnDate(Boolean.FALSE, Boolean.TRUE, new Date());
		assertMessagesOnly(response);
	}	
	
	public void testReturnWithDateWithMessages() throws Exception {
		DateServiceResponse response = delegate.returnDate(Boolean.TRUE, Boolean.TRUE, new Date());
		assertPrimitiveAndMessages(response);
	}	
	
	
	//INTEGER TESTS
	public void testReturnWithIntegerWithoutMessages() throws Exception {
		IntegerServiceResponse response = delegate.returnInteger(Boolean.TRUE, Boolean.FALSE, new Integer("1"));
		assertPrimitiveOnly(response);
	}
	
	public void testReturnWithoutIntegerWithoutMessages() throws Exception {
		IntegerServiceResponse response = delegate.returnInteger(Boolean.FALSE, Boolean.FALSE, new Integer("1"));
		assertNullPrimitiveNullMessages(response);
	}	
	
	public void testReturnWithoutIntegerWithMessages() throws Exception {
		IntegerServiceResponse response = delegate.returnInteger(Boolean.FALSE, Boolean.TRUE, new Integer("1"));
		assertMessagesOnly(response);
	}	
	
	public void testReturnWithIntegerWithMessages() throws Exception {
		IntegerServiceResponse response = delegate.returnInteger(Boolean.TRUE, Boolean.TRUE, new Integer("1"));
		assertPrimitiveAndMessages(response);
	}	
	
	
	//LONG TESTS
	public void testReturnWithLongWithoutMessages() throws Exception {
		LongServiceResponse response = delegate.returnLong(Boolean.TRUE, Boolean.FALSE, new Long("1"));
		assertPrimitiveOnly(response);
	}
	
	public void testReturnWithoutLongWithoutMessages() throws Exception {
		LongServiceResponse response = delegate.returnLong(Boolean.FALSE, Boolean.FALSE, new Long("1"));
		assertNullPrimitiveNullMessages(response);
	}	
	
	public void testReturnWithoutLongWithMessages() throws Exception {
		LongServiceResponse response = delegate.returnLong(Boolean.FALSE, Boolean.TRUE, new Long("1"));
		assertMessagesOnly(response);
	}	
	
	public void testReturnWithLongWithMessages() throws Exception {
		LongServiceResponse response = delegate.returnLong(Boolean.TRUE, Boolean.TRUE, new Long("1"));
		assertPrimitiveAndMessages(response);
	}
	

	//STRING TESTS
	public void testReturnWithStringWithoutMessages() throws Exception {
		StringServiceResponse response = delegate.returnString(Boolean.TRUE, Boolean.FALSE, "");
		assertPrimitiveOnly(response);
	}
	
	public void testReturnWithoutStringWithoutMessages() throws Exception {
		StringServiceResponse response = delegate.returnString(Boolean.FALSE, Boolean.FALSE, "");
		assertNullPrimitiveNullMessages(response);
	}	
	
	public void testReturnWithoutStringWithMessages() throws Exception {
		StringServiceResponse response = delegate.returnString(Boolean.FALSE, Boolean.TRUE, "");
		assertMessagesOnly(response);
	}	
	
	public void testReturnWithStringWithMessages() throws Exception {
		StringServiceResponse response = delegate.returnString(Boolean.TRUE, Boolean.TRUE, "");
		assertPrimitiveAndMessages(response);
	}
	
	
	//TIMESTAMP TESTS
	public void testReturnWithTimestampWithoutMessages() throws Exception {
		TimestampServiceResponse response = delegate.returnTimestamp(Boolean.TRUE, Boolean.FALSE, new Timestamp(System.currentTimeMillis()));
		assertPrimitiveOnly(response);
	}
	
	public void testReturnWithoutTimestampWithoutMessages() throws Exception {
		TimestampServiceResponse response = delegate.returnTimestamp(Boolean.FALSE, Boolean.FALSE, new Timestamp(System.currentTimeMillis()));
		assertNullPrimitiveNullMessages(response);
	}	
	
	public void testReturnWithoutTimestampWithMessages() throws Exception {
		TimestampServiceResponse response = delegate.returnTimestamp(Boolean.FALSE, Boolean.TRUE, new Timestamp(System.currentTimeMillis()));
		assertMessagesOnly(response);
	}	
	
	public void testReturnWithTimestampWithMessages() throws Exception {
		TimestampServiceResponse response = delegate.returnTimestamp(Boolean.TRUE, Boolean.TRUE, new Timestamp(System.currentTimeMillis()));
		assertPrimitiveAndMessages(response);
	}	
	
	
	//VOID TESTS
	public void testReturnVoidWithoutMessages() throws Exception {
		VoidServiceResponse response = delegate.returnVoid(Boolean.FALSE);
		assertNotNull("Message SHOULD have been returned!", response.getMessages());
	}	
	
	public void testReturnVoidWithMessages() throws Exception {
		VoidServiceResponse response = delegate.returnVoid(Boolean.TRUE);
		assertNotNull("Message SHOULD have been returned!", response.getMessages());
	}	
	
	
	/**
	 * Performs assertions for the primitive only outcome 
	 * @param response
	 */
	private void assertPrimitiveOnly(ValueServiceResponse response) {
		assertNotNull("A primitive SHOULD have been returned", response.getValue());
		assertNotNull("Message SHOULD have been returned!", response.getMessages());
	}
	
	/**
	 * Performs assertions for the all null outcome 
	 * @param response
	 */
	private void assertNullPrimitiveNullMessages(ValueServiceResponse response) {
		assertNull("A primistive should NOT have been returned", response.getValue());
		assertNotNull("Message SHOULD have been returned!", response.getMessages());
	}
	
	/**
	 * Performs assertions for the messages only outcome 
	 * @param response
	 */
	private void assertMessagesOnly(ValueServiceResponse response) {
		assertNull("A primistive should NOT have been returned", response.getValue());
		assertNotNull("Message SHOULD have been returned!", response.getMessages());
	}
	
	/**
	 * Performs assertions for the primitive and messages outcome 
	 * @param response
	 */
	private void assertPrimitiveAndMessages(ValueServiceResponse response) {
		assertNotNull("A primistive SHOULD have been returned", response.getValue());
		assertNotNull("Message SHOULD have been returned!", response.getMessages());
	}		
	
}
